package com.wson.controller.clouddisk.framework;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.wson.adapter.clouddisk.factory.CodiskAdapterFactory;
import com.wson.adapter.codisk.ICodiskAdapter;
import com.wson.controller.oauth.AbstractOAuthActivationController;
import com.wson.controller.util.RegistrationUtil;
import com.wson.model.adapter.oauthaccount.IOAuthAccountView;
import com.wson.model.adapter.oauthprovider.IOAuthProviderView;
import com.wson.model.clouddisk.account.ICodiskAccountManip;
import com.wson.model.clouddisk.account.ICodiskAccountMeta;
import com.wson.model.clouddisk.account.ICodiskAccountView;
import com.wson.model.clouddisk.provider.ICodiskProviderManip;
import com.wson.model.clouddisk.provider.ICodiskProviderMeta;
import com.wson.model.clouddisk.provider.ICodiskProviderView;
import com.wson.model.framework.identity.IIdentityManip;
import com.wson.model.framework.identity.IIdentityView;
import com.wson.platform.factory.impl.BeanFactory;
import com.wson.root.util.LoggerUtil;
import com.wson.util.throwable.BasicException;

public abstract class AbstractCodiskActivationController extends AbstractOAuthActivationController{
	@Override
	protected IOAuthProviderView generateProvider(HttpServletRequest request)throws Exception {

		Long providerid = this.retrieveRequestParamLong(request, ICodiskProviderMeta.SYNC_SELF);
		
		ICodiskProviderView provider = (ICodiskProviderView)BeanFactory.getCurrentFactory().getModelFactory().getModel(ICodiskProviderMeta.META_ID);		
		this.initModel(request, provider);
		((ICodiskProviderManip)provider).setSessionProfile(sessionProfile);
		((ICodiskProviderManip)provider).setID(providerid);			
		if(provider.getID()!=null 
				&& provider.selfSelect())
			return provider;
		else
			LoggerUtil.WARNING("provider "+provider.getID()+" is not existed!");

		return null;
	}

	@Override
	protected IOAuthAccountView generateOAuthAccount(HttpServletRequest request) throws Exception{
		ICodiskAdapter adapter = CodiskAdapterFactory.getAdapter((ICodiskProviderView)provider);
		Map<String, Object> params = new HashMap<String, Object>();
		//this.printRequestParams(request);
		params.put(ICodiskAdapter.REQUESTTOKEN, request.getParameter("code"));
		
		LoggerUtil.DEBUG("params "+params);

		ICodiskAccountView account = (ICodiskAccountView)adapter.generateAccessAccount(accountCertificationResponseAction(request), params);
		return account;
		
	}
	
	protected  abstract String accountCertificationResponseAction(HttpServletRequest request);

	@Override
	protected IOAuthAccountView generateCheckAccount(IOAuthAccountView account, HttpServletRequest request)throws Exception {
		ICodiskAccountView accountcheck = (ICodiskAccountView)BeanFactory.getCurrentFactory().getModelFactory().getModel(ICodiskAccountMeta.META_ID);
		account.initModel(accountcheck);
		((ICodiskAccountManip)accountcheck).setProviderID(account.getProviderID());
		((ICodiskAccountManip)accountcheck).setAccessID(account.getAccessID());
		((ICodiskAccountManip)accountcheck).setCertified(true);
		//((ICodiskAccountManip)accountcheck).setOauthToken(account.getOauthToken());
		//((ICodiskAccountManip)accountcheck).setOauthSecret(account.getOauthSecret());

		if(accountcheck.getAccessID()!=null 
				&& account.getProviderID()!=null
				&& accountcheck.selfSelect()){}
		else
			LoggerUtil.WARNING("account "+accountcheck.getAccessID()+" "+account.getProviderID()+" is not existed!");
			//LoggerUtil.DEBUG("accountcheck.getID() "+accountcheck.getID());

		return accountcheck;
	}

	@Override
	protected boolean externalRegistration(IOAuthAccountView account,
			HttpServletRequest request) throws Exception {
		
/*		if(account==null 
				|| account.getAccessID()==null)
			return false;*/
		
		boolean result = true;
		
		//ICodiskAdaptor adapter = CodiskAdaptorFactory.getAdapter((ICodiskAccountView)account,(ICodiskProviderView)provider);
		
		String authname = ((ICodiskAccountView)account).getAccountName();//adapter.generateRegistrationName((ICodiskAccountView)account);
		
		if(authname==null)
			throw new BasicException("NoSession");
		
		RegistrationUtil util = new RegistrationUtil();
		
		util.setApplication(this.application);

		
		
		//check if agent existed?
		this.identity=(IIdentityView)BeanFactory.getCurrentFactory().getModelFactory().getModel(IIdentityView.META_ID);
		this.initModel(request, identity);
		((IIdentityManip)identity).setAuthname(authname);
		LoggerUtil.DEBUG("authname "+identity.getAuthname());
		//LoggerUtil.DEBUG("authname "+authname);
		//((IIdentityManip)identity).setPassword(this.retrieveRequestModelParam(request, IIdentityView.SYNC_PASSWORD));
		boolean identityexisted = false;
		if(identity.getAuthname()!=null)
			identityexisted = identity.selfSelect();
			
		//LoggerUtil.DEBUG("identity existed "+(identity.getID()!=null));
		if(!identityexisted){//identity.getID()==null){			
			
			((IIdentityManip)identity).setAuthname(authname);
			((IIdentityManip)identity).setPassword(account.getAccountName());
			
			util.setIdentity(identity);
			
			result = util.registration();
			
			if(result){
				
				
			
			
				identity = util.getIdentity();
				LoggerUtil.DEBUG("identity "+identity.getID());
				((ICodiskAccountManip)account).setIdentityID(identity.getID());
				((ICodiskAccountManip)account).setShortText(authname);
				//((ICodiskAccountManip)account).setAgentID(agent.getID());
				
				((ICodiskAccountManip)account).selfCreate();
				
				LoggerUtil.DEBUG("account "+account.getID()); 
			}
			
		}
		return result;
	}

}
